/******************************************************************************
COPYRIGHT 2012   : ATECH
Layer            : Data Link Layer(DLL)
Source File Name : Slave_lin1_dll2.c
Group            : SoftWare Team
Author           : 
Date First Issued: 12/18/2012                                                  
******************************************************************************/
#define Slave_lin1_dll2_c

/* #include */
#include "Slave_lin1_cfg.h"
#include "Slave_lin1_syst.h"
#include "Slave_lin1_dtype.h"
#include "Slave_lin1_htype.h"
#include "Slave_lin1_appl.h"
#include "Slave_lin1_apis.h"
#include <string.h>
/* replace yourself head files */
#include "derivative.h"


extern l_u8 Temp_Blower_upDown;
extern l_u8 Temp_Blower_gap;
extern l_u8 Temp_Temp_upDown;
extern l_u8 Temp_Temp_gap;
/* variables */
l_u8 LIN1_BytesToSent     = 0;
l_u8 LIN1_BytesToReceived = 0;

/* arrays */
/* 8+1(DATAmax+CHECKSUM) */
l_const l_u8 LIN1_Sleep_Frame[1+SLEEP_FRAME_LENTH] =
{
    LIN1_SLEEP_FRAME_COMMAND
};
 
/* 8+1(DATAmax+CHECKSUM) */
l_u8 LIN1_Sleep_Frame_Rx_Buffer[1+SLEEP_FRAME_LENTH];


/*****************************************************************************/
/**
 * Function Name: l_u8 lin1_ifc_send_message(void)
 * Description: send DATAmax+CHECKSUM  
 * Slave node - when the ID for the frame is received  
 * Param:   none
 * Return:  yes or no
 * Author:  
 *****************************************************************************/
l_u8 lin1_ifc_send_message(void)
{
    l_u8 i;
    
#if(SLAVE_LIN1_CHANNEL == CHANNEL_SCI0)    
    if(SCI0SR1 & SCI0SR1_RDRF_MASK) (void)LIN1_UART_RECEIVE_BUF;
#else
    if(SCI1SR1 & SCI1SR1_RDRF_MASK) (void)LIN1_UART_RECEIVE_BUF;
#endif

}

/*****************************************************************************/
/**
 * Function Name: l_u8 lin1_ifc_receive_message(void)
 * Description: recevie DATAmax+CHECKSUM  
 * Slave node - when the checksum for the received frame is validated. The
 * slave node updates its received signals directly after the frame is finished
 * (i.e.at interrupt level). 
 * Param:   none
 * Return:  yes or no
 * Author:  
 *****************************************************************************/
l_u8 lin1_ifc_receive_message(void)
{
    l_u8 i;
    
    /* CHECKSUM error! */
    if(LIN1_Rx_Buffer[LIN1_ID_Table[LIN1_CurrIdIndex].length]!=\
    lin1_checksum(LIN1_Rx_Buffer,LIN1_ID_Table[LIN1_CurrIdIndex].length,\
                                 LIN1_ID_Table[LIN1_CurrIdIndex].id))
    return L_ERROR_CHECKSUM;
     
    /* load data from rx buffer */
    for(i=0;i<LIN1_ID_Table[LIN1_CurrIdIndex].length;i++)
    {
        *(LIN1_ID_Table[LIN1_CurrIdIndex].data_ptr+i)=LIN1_Rx_Buffer[i];
    }
    if(LIN1_ID_Table[LIN1_CurrIdIndex].id == DIAG_REQUEST_ID)
    {
        if(memcmp(LIN1_ID_Table[LIN1_CurrIdIndex].data_ptr, LIN1_Sleep_Frame, SLEEP_FRAME_LENTH) == 0)
        {
            lin1_sys_vSetEvent(LIN1_EVENT_RecievedSleepCommand);    
        }
        else
        {
            lin1_ld_rx_into_raw(LIN1_ID_Table[LIN1_CurrIdIndex].data_ptr);    
        }     
    }
    return L_OK;
}

 /*****************************************************************************/
/**
 * Function Name: void lin1_ifc_wake_Up(void)
 * Description:  
 *   
 * Param:   none
 * Return:  
 * Author:  
 *****************************************************************************/
 void lin1_ifc_wake_Up(void)
 {
    LIN1_SEND_BREAK();    
 }

/*****************************************************************************/
/**
 * Function Name: void lin1_ifc_goto_sleep(void)
 * Description:  
 *   
 * Param:   none
 * Return:  
 * Author:  
 *****************************************************************************/
 void lin1_ifc_goto_sleep(void)
 {
    if(memcmp(LIN1_Sleep_Frame_Rx_Buffer, LIN1_Sleep_Frame, SLEEP_FRAME_LENTH+1) == 0)
    {
        lin1_sys_vSetEvent(LIN1_EVENT_RecievedSleepCommand);    
    }   
 }
 
/*****************************************************************************/
/**
 * Function Name: l_u8 lin1_ifc_break_action(void)
 * Description: when detect one break 
 *   
 * Param:   none
 * Return:  yes or no
 * Author:  
 *****************************************************************************/
l_u8 lin1_ifc_break_action(void)
{
    /* ready to receive */      
    LIN1_BytesToReceived=0;
    
    if(LIN1_CurrIdIndex==L_ID_INDEX_NOTHING) return L_NO;
    if(LIN1_ID_Table[LIN1_CurrIdIndex].dir==LIN1_ID_DIRECTION_TX) return L_NO;
    	
	return L_YES;		
}